package com.swufe.team;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {


    private String TAG = "RateList2";
    private ListView listView;

    @Override


        protected void onCreate(Bundle savedInstanceState) {
            ArrayList<HashMap<String,String>>listItems = null;
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rate_list2);
        //准备测试数据
//        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
//        for (int i = 0; i < 30; i++) {
//            HashMap<String, String> map = new HashMap<String, String>();
//            map.put("ItemTitle", "Rate: " + i);//标题
//            map.put("ItemDetail", "Datail: " + i);//内容
//            listItems.add(map);

            //生成适配器的Item和动态数组对应的元素
            SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems, R.layout.list_item,
                    new String[]{"ItemTitle", "ItemDetail"},
                    new int[]{R.id.itemTitle, R.id.itemDetail});
//            listItems  =new ArrayList<HashMap<String,String>>();

            MyAdapter myAdapter = new MyAdapter(this, R.layout.list_item, listItems);
            listView = findViewById(R.id.Ratelist);
            listView.setAdapter(myAdapter);
            listView.setOnItemLongClickListener(this);



            Handler handler = new Handler(Looper.myLooper()) {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    if (msg.what == 7) {
                        ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                        //用户刷新列表
                        MyAdapter myAdapter = new MyAdapter(RateListActivity2.this, R.layout.list_item, list);
                        listView.setAdapter(myAdapter);
                    }
                    super.handleMessage(msg);
                }
            };

            MapTask mapTask = new MapTask();
            mapTask.setHandler(handler);

            Thread t = new Thread(mapTask);
            t.start();
        }



        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            TextView title =(TextView) view.findViewById(R.id.itemTitle);
            TextView detail =(TextView) view.findViewById(R.id.itemTitle);
            String title2=String.valueOf(title.getText());
            String detail2=String.valueOf(detail.getText());
            Log.i(TAG,"onTtemClick: title2="+title2);
            Log.i(TAG,"onTtemClick: detail2="+detail2);

            Intent Rate = new Intent(RateListActivity2.this, RateOption.class);
            Rate.putExtra("Rate_Title",100f/Float.parseFloat(detail2));
            Rate.putExtra("Rate_Value",title2);
            startActivityForResult(Rate,8);


        }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap <String,String>map=(HashMap<String, String>) listView.getItemAtPosition(position);

        return true;//屏蔽单击事件
    }
}